package org.dong.Controller;

import com.alibaba.fastjson.JSON;
import org.dong.Dao.TranDaoImpl;
import org.dong.Pojo.Dto;
import org.dong.Pojo.HttpRequest;
import org.dong.Pojo.HttpResponse;
import org.dong.Tool.Data;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class DownloadController implements ServerLet{
     @Override
    public void server(HttpRequest request, OutputStream os) {
        Dto dto = new Dto();
        String msg = "ok";

        //查找数据中是否存在此记录

        String[] list = request.getRequestData().split("&");
        //获取文件ID
        int id = Integer.parseInt(list[0].split("=")[1]);
         Map<String,String> map = new HashMap<>();
         map.put("file_id",id+"");
         map.put("user_id",Data.user.getId()+"");
         map.put("type",0+"");
        if(new TranDaoImpl().addDownload(map)){
            dto.setErrMsg("下载成功");
            dto.setStatusCode(2000);
        }else{
            dto.setStatusCode(2001);
            dto.setErrMsg("下载失败");
        }

        msg = JSON.toJSONString(dto);

         HttpResponse response = new HttpResponse(os,request.getUrl(),msg);

     }
}
